/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package clientClasses;

import commonClasses.*;
import commonClasses.Messages.EnterAuctionMsg;
import commonClasses.Messages.Msg;
import commonClasses.Messages.NakMsg;
import commonClasses.commonInterfaces.ClientModelIntf;
import commonClasses.commonInterfaces.ItemIntf;
import commonClasses.commonInterfaces.ServerIntf;
import commonClasses.commonInterfaces.ProtocolIntf;

/**
 *
 * @author Pozytywny!
 */
public class EnterAuctionOrder {

    private ProtocolIntf _clientProtocol = null;
    private ClientModelIntf _model = null;
    private long _itemID;

    public EnterAuctionOrder(ProtocolIntf cp, long itemID) {
        _clientProtocol = cp;
        _model = _clientProtocol.getClientModel();
        _itemID = itemID;
    }

    public void go() {
        try {
            new EnterAuctionMsg(_clientProtocol, _itemID).send();

            long receivedMsgID = Msg.receiveMsgId(_clientProtocol.getDIS());

            if (receivedMsgID == MagicNumbers.ACK_MSG_NR) {
                //no to ok.
                ServerIntf server = _model.getServerByAddAndPort(
                        _clientProtocol.getIP(),
                        _clientProtocol.getPort());

                if (server != null) {

                    ItemIntf item = server.getItemByID(_itemID);
                    if (item != null) {
                        item.setAuctionEntered();
                    } else {
                        Utils.write("nie znalazłem item w EnterAuctionOrder");
                    }

                } else {
                    Utils.write("nie znalazłem serwera w EnterAuctionOrder");
                }

            } else if (receivedMsgID == MagicNumbers.NAK_MSG_NR) {
                NakMsg nakMsg = new NakMsg(_clientProtocol);
                nakMsg.receive();
                Utils.write("Odrzucono wejście do aukcji bo:" + nakMsg.getReason());
            } else {
                Utils.write("otrzymałem newłaściwy mesg w EnterAuction order:" +
                        receivedMsgID);
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
